<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Kaisan extends MY_Controller {

    function __construct() {
        parent::__construct();
        
    }

    function index() {
        $type  = isset($_GET['type']) ? $_GET['type'] : 1;
//        $this->memcached->save("kaisan_current_data","");//缓存7天
        if($type == 1){
            $list = $this->db->query("select expectdate,opencode,opentime,expect_daxiao,daxiao_status,status from tan_kaisan order by id desc limit 500")->result_array();
        }else{
            $list = $this->db->query("select expectdate,opencode,opentime,expect_danshuang,danshuang_status,status from tan_kaisan order by id desc limit 500")->result_array();
        }
        $list = array_reverse($list);
        $this->assign("list", $list);
        $this->assign("t", $type);
        $this->display('kaisan/index.html');
    }
    
    
    function task(){
//        file_put_contents('/log/run.log', date('Y-m-d H:i:s')."\r\n", FILE_APPEND);
        
        $cache_config = $this->memcached->get("kaisan_config");
        if(!$cache_config){
               $kaisan_config = $this->db->query("select * from tan_kaisan_config where id = 1 limit 1")->row_array();
               $kaisan_config['daxiao_config'] = explode(",", $kaisan_config['daxiao_config']);
                $kaisan_config['danshuang_config'] = explode(",", $kaisan_config['danshuang_config']);
                $cache_config = $kaisan_config;
                $this->memcached->save("kaisan_config",serialize($kaisan_config),86400 * 7);//缓存7天
        }else{
            $cache_config  = unserialize($cache_config);
        }
        $kaisan_current_data = $this->memcached->get("kaisan_current_data");
        
        if(!$kaisan_current_data){
            $expect_data = $this->db->query("select expectdate,expect_num from tan_kaisan where status = 0 order by id desc limit 1")->row_array();
             $kaisan_current_data = $expect_data;
             $this->memcached->save("kaisan_current_data",serialize($kaisan_current_data),86400 * 1);//缓存7天
        }else{
            $kaisan_current_data  = unserialize($kaisan_current_data);
        }
//        p($kaisan_current_data);die;
        $http_url = "http://b.apiplus.net/newly.do?token=b3c88aa149a5764e&code=ahk3&format=json";
        $result = file_get_contents($http_url);
        $result = json_decode($result, true);
        
        $current_data = $result['data'][0];//当前获取最新数据
        //开奖
        if($current_data['expect'] ==$kaisan_current_data['expectdate'] ){
            
            $current = $this->db->query("select * from tan_kaisan where expectdate = '$kaisan_current_data[expectdate]' order by id desc limit 1")->row_array();
            $config =  $this->db->query("select * from tan_kaisan_config where id  = 1 order by id desc limit 1")->row_array();
            
            $code_arr = explode(",", $current_data['opencode']);
            $size = $code_arr[0] + $code_arr[1] + $code_arr[2];
            
             $update_data = array();//更新开奖数据
             $update_data['opencode']  =  $current_data['opencode'];
            $update_data['opentime']  =  $current_data['opentime'];
            $update_data['opentimestamp']  =  $current_data['opentimestamp'];
            
            $update_config = array();
            
            $daxiao_status = 0;//状态
            $danshuang_status = 0;//状态
            
            if($size  > 10){//判断大小
                $update_data['true_daxiao'] = "大";
            }else{
                $update_data['true_daxiao'] = "小";
            }
            
            if($update_data['true_daxiao']  == $current['expect_daxiao']){
                $daxiao_status = 1;
                $update_config ['daxiao_yes_count'] = $config['daxiao_yes_count'] + 1;
                $update_config['daxiao_no_count'] = 0;
            }else{
                $update_config ['daxiao_yes_count'] = 0;
                $update_config['daxiao_no_count'] = $config['daxiao_no_count'] + 1;
            }
            $update_data['daxiao_status'] = $daxiao_status;
            
            if((int)$size % 2 ==0){//判断单双
                    $update_data['true_danshuang'] = "双";
            }else{
                    $update_data['true_danshuang'] = "单";
            }
            
             if($update_data['true_danshuang']  == $current['expect_danshuang']){
                $danshuang_status = 1;
                $update_config['danshuang_yes_count'] = $config['danshuang_yes_count'] + 1;
                 $update_config['danshuang_no_count'] = 0;
            }else{
                $update_config['danshuang_yes_count'] = 0;
                 $update_config['danshuang_no_count'] = $config['danshuang_no_count'] + 1;
            }
            $update_data['danshuang_status'] = $danshuang_status;
               
            $update_data['status'] = 1;
            
            $update_config['max_daxiao_yes_count'] = $update_config ['daxiao_yes_count'] > $config['max_daxiao_yes_count']  ? $update_config ['daxiao_yes_count'] : $config['max_daxiao_yes_count'];
            $update_config['max_daxiao_no_count'] = $update_config ['daxiao_no_count'] > $config['max_daxiao_no_count']  ? $update_config ['daxiao_no_count'] : $config['max_daxiao_no_count'];
            $update_config['max_danshuang_yes_count'] = $update_config ['danshuang_yes_count'] > $config['max_danshuang_yes_count']  ? $update_config ['danshuang_yes_count'] : $config['max_danshuang_yes_count'];
            $update_config['max_danshuang_no_count'] = $update_config ['danshuang_no_count'] > $config['max_danshuang_no_count']  ? $update_config ['danshuang_no_count'] : $config['max_danshuang_no_count'];
            
               //更新数据
               
               $result1 = $this->db->where("id",$current['id'])->update("tan_kaisan",$update_data);
               $result2 = $this->db->where("id",1)->update("tan_kaisan_config",$update_config);
               if($result1 && $result2){
                    $next_data = array();
                   
                   //开始下一轮
                   if($current['expect_num']  == 80){
                            $next_data['expect_num'] = 1;
                           $next_data['expectdate'] = date("Ymd",strtotime("+1 day"))."001";
                   }else{
                            $next_data['expect_num'] = $current['expect_num'] + 1;
                             $next_data['expectdate'] = date("Ymd").($next_data['expect_num'] > 9? "0".$next_data['expect_num'] :"00".$next_data['expect_num']);
                   }
                   $next_data['expect_daxiao'] = $cache_config['daxiao_config'][$next_data['expect_num'] - 1];
                   $next_data['expect_danshuang'] = $cache_config['danshuang_config'][$next_data['expect_num'] - 1];
                   
                   $this->db->insert("tan_kaisan",$next_data);
                   $this->memcached->save("kaisan_current_data",serialize(array("expectdate"=>$next_data['expectdate'],"expect_num"=>$next_data['expect_num'] )),86400 * 1);//缓存1天
                   
               }
        }
    }
    
    function ajax(){
        if($_POST){
            
        }
    }
}
